/**
 * 
 */
package com.tvshows.structure.reader;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.log4j.Logger;

import com.tvshows.structure.bdd.BddQueries;
import com.tvshows.structure.beans.Properties;
import com.tvshows.structure.log.LoggingTreatment;

/**
 * @author Tangi Midy
 * <p>
 * Operations on Configuration Files
 * </p>
 */
public class FileOperations
{

    /**
     * The file to read
     */
    private File file;

    /**
     * The file reader
     */
    private FileReader fileReader;

    /**
     * The file Writer
     */
    private FileWriter fileWriter;

    /**
     * The line to analyze
     */
    private String line;

    /**
     * The buffered reader
     */
    private BufferedReader bufferedReader;

    /**
     * The list of properties
     */
    private final ArrayList<String> properties;

    /**
     * The path of the property
     */
    private String propertyPath;

    /**
     * The logger of the class
     */
    private final Logger logger = Logger.getLogger( FileOperations.class );

    /**
     * The {@link FileOperations} instance
     */
    private static FileOperations fileOperations;

    /**
     * Constructor of the class
     */
    public FileOperations()
    {
        properties = new ArrayList<String>();
    }

    /**
     * Reads the configuration
     * 
     * @return list of properties
     */
    public ArrayList<String> readProperties()
    {
        logger.info( "Lecture des propriétés " );

        if ( properties.isEmpty() )
        {
            // Accès au path des propriétés
            propertyPath = BddQueries.getBddQueries().getPropertyPath();

            if ( null == propertyPath )
            {
                file = new File( "src/main/resources/tvshows.properties" );
            }
            else
            {
                file = new File( propertyPath );
            }

            try
            {
                fileReader = new FileReader( file );
                bufferedReader = new BufferedReader( fileReader );

                while ( bufferedReader.read() != -1 )
                {
                    line = bufferedReader.readLine();
                    if ( null != line )
                    {
                        if ( line.contains( "TORRENT_SOFT_PATH" ) )
                        {
                            properties.add( line.split( "=" )[1] );
                        }
                        if ( line.contains( "HIBERNATE_CONF" ) )
                        {
                            properties.add( line.split( "=" )[1] );
                        }
                    }
                }

                bufferedReader.close();
                fileReader.close();
            }
            catch ( FileNotFoundException e )
            {
                LoggingTreatment.getInstance().logError( logger, e.getMessage() );
            }
            catch ( IOException e )
            {
                LoggingTreatment.getInstance().logError( logger, e.getMessage() );
            }
        }

        return properties;
    }

    /**
     * Changes the configuration
     * 
     * @param changesList the list of changes
     */
    public void writeProperties( Properties changesList )
    {
        try
        {
            fileReader = new FileReader( file );
            bufferedReader = new BufferedReader( fileReader );
            String actualText = "";

            while ( bufferedReader.read() != -1 )
            {
                line = bufferedReader.readLine();
                if ( line.contains( "TORRENT_SOFT_PATH" ) && null != changesList.getSoftwarePath() )
                {
                    line = line.substring( 0, line.indexOf( "=" ) + 1 ) + changesList.getSoftwarePath();
                }

                if ( line.contains( "HIBERNATE_CONF" ) && null != changesList.getHibernatePath() )
                {
                    line = line.substring( 0, line.indexOf( "=" ) + 1 ) + changesList.getHibernatePath();
                }

                actualText += "_" + line + "\n";
            }
            bufferedReader.close();
            fileReader.close();

            fileWriter = new FileWriter( file );
            fileWriter.write( actualText );
            fileWriter.close();

        }
        catch ( FileNotFoundException e )
        {
            LoggingTreatment.getInstance().logError( logger, e.getMessage() );
        }
        catch ( IOException e )
        {
            LoggingTreatment.getInstance().logError( logger, e.getMessage() );
        }
    }

    /**
     * @return the property file path
     */
    public String getPropertyPath()
    {
        if ( propertyPath != null )
        {
            return propertyPath;
        }
        return "";

    }

    /**
     * Changes the fileName
     * 
     * @param text the name of the file
     */
    public void changeFile( String text )
    {
        file = new File( text );
    }

    /**
     * @return the singleton {@link FileOperations}
     */
    public static FileOperations getInstance()
    {
        if ( fileOperations == null )
        {
            fileOperations = new FileOperations();
        }
        return fileOperations;
    }

}
